package com.sgb.jicai.viewmodel;

import android.util.Log;
import android.view.View;
import androidx.lifecycle.ViewModel;
import com.google.gson.Gson;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.sgb.jicai.R;
import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.link.entity.BrandEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.util.GlideEngine;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.jicai.entity.JiCaiPublishListBean;
import com.sgb.jicai.entity.SupplierPublishBean;
import com.sgb.jicai.view.activity.JiCaiSupplierPublishAct;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Time:2021/7/19
 * AUTHOR:F_Q
 * Description:
 */
public class SupplierPublishModel extends ViewModel {
    JiCaiSupplierPublishAct activity;
    public void pictureSelector(int number, int requestCode) {
        PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofImage())
                .loadImageEngine(GlideEngine.createGlideEngine())
                .theme(R.style.picture_default_style)
                .selectionMode(PictureConfig.MULTIPLE)
                .compress(true)//是否压缩
                .minimumCompressSize(3413)//小于3m不压缩
                .maxSelectNum(number)//判断还能选多少张图片
                .forResult(requestCode);
    }

    /**
     * 公司列表信息
     */
    public void getCompanyList(View view,boolean isShow) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getMyCompanyList().enqueue(new ICallBack<BaseEntity<List<JiCaiPublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<JiCaiPublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    if (data.getData().size()>0){
                        activity.setCompanyList(data.getData(),view,isShow);
                    }else {
                        MToast.showToast(activity,"当前用户没有其它公司信息");
                    }
                }
            }
        });
    }

    /**
     * 部门列表信息
     */
    public void getDepartmentList(String companyNo,View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getDeptList(companyNo).enqueue(new ICallBack<BaseEntity<List<JiCaiPublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<JiCaiPublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    if (data.getData().size()>0){
                        activity.setDepartmentList(data.getData(),view);
                    }else {
                        MToast.showToast(activity,"当前单位无部门信息");
                    }
                }
            }
        });
    }

    /**
     * 获取地址信息
     */
    public void getAddressList(View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.initAddressData(data.getData(),view);
                }
            }
        });
    }

    /**
     * 获取用户列表数据
     */
    public void getUserList(View view,boolean isboole) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getUserList().enqueue(new ICallBack<BaseEntity<List<JiCaiPublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<JiCaiPublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.setUserList(data.getData(),view,isboole);
                }
            }
        });

    }

    /**
     *  获取付款方式
     */
    public void getPaymentType(View view) {
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("预付款","","1"));
        bean.add(new JiCaiPublishListBean("货到付款","","2"));
        bean.add(new JiCaiPublishListBean("其他","","3"));
        activity.setPaymentList(bean,view);
    }

    /**
     *发票类型
     */
    public void getInvoiceType(View view) {
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("不带票","","1"));
        bean.add(new JiCaiPublishListBean("增值税专用票","","2"));
        bean.add(new JiCaiPublishListBean("增值税普通发票", "", "3"));
        activity.setInvoiceList(bean,view);
    }

    /**
     * 税率
     */
    public void getTaxRate(View view) {
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("3%","",""));
        bean.add(new JiCaiPublishListBean("6%","",""));
        bean.add(new JiCaiPublishListBean("9%","",""));
        bean.add(new JiCaiPublishListBean("11%","",""));
        bean.add(new JiCaiPublishListBean("13%","",""));
        activity.setTaxRateList(bean,view);
    }

    /**
     * 单位
     */
    public void getUnitList(View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
       JiCaiBaseApi.getApi().getfindUnitList().enqueue(new ICallBack<BaseEntity<List<JiCaiPublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<JiCaiPublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    if (data.getData().size()>0){
                        activity.setUnitList(data.getData(),view);
                    }else {
                        MToast.showToast(activity,"没有单位信息");
                    }
                }
            }
        });
    }

    /**
     * 品牌范围
     */
    public void getRangeBrandsList(String thirdType,View view) {
        String companyId = SharedPreferenceUtil.getInstance().getSaveStringData(com.swgk.core.util.SharedPreferenceUtil.REAL_ID, "");
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getBrandList(thirdType, "", companyId).enqueue(new ICallBack<BaseEntity<List<BrandEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<BrandEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null) {
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.showBrandPop(data.getData(),view);
                }
            }
        });
    }

    /**
     * 获取商品列表
     */
    public void getGoodsCategory() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getGoodsTreeNodeByProvinceIdV2().enqueue(new ICallBack<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.showGoodsTypePop(data.getData());
                }
            }
        });
    }

    //上传图片
    public void uploadAvatarFileModel(File file, int completeNum, SupplierPublishBean oneBean) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + completeNum + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, null, "上传中" + "...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    MToast.showToast(activity, "上传失败！");
                    return;
                }
                if (response.body().getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.uploadAvatarResult(response.body().getData().toString(),oneBean);
                } else {
                    MToast.showToast(activity, "上传失败！");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, "上传失败！" + t.getMessage());
            }
        });
    }

    //供方发起发布接口
    public void SupplierPublish(SupplierPublishBean subBean) {
        Log.e("发布接口数据",new Gson().toJson(subBean));
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
       JiCaiBaseApi.getApi().getaddProductSale(subBean).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)){
                    MToast.showToast(activity,"发布成功");
                    activity.SucceedView();
                }else {
                   MToast.showToast(activity,data.getMsg());
                }
            }
        });
    }

    public void setActivity(JiCaiSupplierPublishAct jiCaiSupplierPublishAct) {
        this.activity = jiCaiSupplierPublishAct;
    }
}
